package IBBB.network.sendmessage;

import server.Word;
import IBBB.model.WordProperty;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class send_query_word extends  send_String {
    public send_query_word(String server, String wordspell) {
        super(server);
        target_message="queryWord#"+wordspell;
    }


    public WordProperty getWordProperty() {
        return wordProperty;
    }

    WordProperty wordProperty;

    @Override
    public Void call() {

        Socket client = new Socket();
        try {
            client.connect(message_socket, 3000);
        } catch (Exception e) {
            updateMessage("timeout");
            return null;
        }


        try (ObjectOutputStream write = new ObjectOutputStream(client.getOutputStream())) {
            try (ObjectInputStream reader = new ObjectInputStream(client.getInputStream())) {
                write.writeObject(target_message);
                Object o = reader.readObject();
                client.close();
                if (o == null) {
                    updateMessage("spellWrong");
                } else {
                    Word w = (Word)o;
                    wordProperty = new WordProperty(w);
                    updateMessage("success");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            updateMessage("wrong");
        }
        return null;
    }
}
